package com.cjc.tree_view;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.cjc.bean.FileBean;
import com.cjc.tree.R;
import com.cjc.tree.bean.Node;
import com.cjc.tree.bean.TreeListViewAdapter;
import com.cjc.tree.bean.TreeListViewAdapter.OnTreeNodeClickListener;

/**
 * 
 ***************************************************** 
 * <hr>
 * <dt><span class="strong">类功能简介:</span></dt>
 * <dd>Coffee</dd>
 * <dt><span class="strong">创建时间:</span></dt>
 * <dd>2015-3-19 上午11:50:17</dd>
 * <dt><span class="strong">公司:</span></dt>
 * <dd>CorpIt</dd>
 * 
 * @author aa1000777 - Email:aa1000777@qq.com
 ***************************************************** 
 */
public class MainActivity extends Activity {
	// data数据集
	private List<FileBean> mDatas = new ArrayList<FileBean>();
	private ListView mTree;
	private TreeListViewAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mTree = (ListView) findViewById(R.id.id_tree);
		try {
			/* 获取数据 */
			initDatas();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			mAdapter = new SimpleTreeAdapter<FileBean>(mTree, this, mDatas, 10);
			mAdapter.setOnTreeNodeClickListener(new OnTreeNodeClickListener() {
				@Override
				public void onClick(Node node, int position) {
					System.out.println("-------------------------------------------");
					if (node.isLeaf()) {
						Toast.makeText(getApplicationContext(), node.getName(), Toast.LENGTH_SHORT).show();
					} else {
						// 滚动到第一行
						// mTree.setSelection(position);
					}
				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}
		mTree.setAdapter(mAdapter);
	}

	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 获取数据
	 * 
	 * @throws IOException
	 ***************************************************** 
	 */
	private void initDatas() throws IOException {
		int _id = 0;
		int parentId = 0;
		String title = "";
		InputStream myInput = this.getAssets().open("Tree.txt");
		InputStreamReader isr = new InputStreamReader(myInput, "utf8");
		// 构造一个BufferedReader类来读取文件
		BufferedReader br = new BufferedReader(isr);
		String str = null;
		while ((str = br.readLine()) != null) {// 使用readLine方法，一次读一行
			int spaceLength = str.indexOf(" ");
			String idAll = str.substring(0, spaceLength);
			// title = str.substring(spaceLength + 1);//只取标题
			title = str;// 包含目录
			int idLength = idAll.indexOf(".");
			if (-1 != idAll.indexOf(".")) {
				_id = Integer.valueOf(idAll.replace(".", "") + 0);
				parentId = Integer.valueOf(idAll.substring(0, idLength));
			} else {
				_id = Integer.valueOf(idAll);
				parentId = 0;
			}
			System.out.println(_id + "          " + parentId + "         " + title);
			mDatas.add(new FileBean(_id, parentId, title));
		}
		br.close();
	}

}
